//
//  ESBaseViewController.m
//  EasyStyle
//
//  Created by FK on 15/12/10.
//  Copyright © 2015年 FK. All rights reserved.
//

#import "ESBaseViewController.h"
#import "ESMainViewController.h"

@interface ESBaseViewController ()
{
    BOOL _statusBarHidden;
}


@end

@implementation ESBaseViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (NSString *)pageName{
//    if ([self isKindOfClass:[SGEmptyViewController class]]) {
//        return [NSStringFromClass([self class]) stringByAppendingFormat:@"_%@",NSStringFromClass([[(SGEmptyViewController *)self contentView] class])];
//    }
//    return NSStringFromClass([self class]);
//}


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
//    if (!_notTrack) {
//        NSString *pageName = [self pageName];
//        //[MTA trackPageViewBegin:pageName];
//        [MobClick beginLogPageView:pageName];
//    }
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
//    if (!_notTrack) {
//        NSString *pageName = [self pageName];
//        //[MTA trackPageViewEnd:pageName];
//        [MobClick endLogPageView:pageName];
//    }
}


- (void)addCustomNavigationBar{
    [self addCustomNavigationBarWithStyle:DefaultStyle customColor:NavigationBarDefultColor];
}

- (void)addCustomNavigationBarWithStyle:(ESCustomNavigationbarStyle)style customColor:(UIColor *)barColor{
    if (!self.sgCustomNavigationBar) {
        ESCustomNavigationbar *navigationBar = [[ESCustomNavigationbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, ShouleAdaptForiOS7?64:44) style:style customColor:barColor];
        self.sgCustomNavigationBar = navigationBar;
    }
    [self.view addSubview:self.sgCustomNavigationBar];
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        [self setNeedsStatusBarAppearanceUpdate];
    }
    if ([self.navigationController.viewControllers count]>1 || self.presentingViewController) {
        //[self.sgCustomNavigationBar showBackButton];
        //[self.sgCustomNavigationBar.backButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    }
    
}

- (void)addToggleNavigationBarGesture{
    //控制顶部栏显示隐藏逻辑
    UIPanGestureRecognizer* panSwipeRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanSwipeDown:)];
    panSwipeRecognizer.delegate               = self.sgCustomNavigationBar;
    panSwipeRecognizer.minimumNumberOfTouches = 1;
    [self.view addGestureRecognizer:panSwipeRecognizer];
}

- (BOOL)setCustomNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated{
    
    
    return [self setCustomNavigationBarHidden:hidden animated:animated complete:nil];
}

- (BOOL)setCustomNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated complete:(void(^)())block{
    if (!self.sgCustomNavigationBar) {
        return NO;
    }
    
    if (hidden && self.sgCustomNavigationBar.frame.origin.y < 0) {
        return NO;
    }
    else if (!hidden && self.sgCustomNavigationBar.frame.origin.y == 0){
        return NO;
    }
    
    [self.sgCustomNavigationBar setNavigationBarHidden:hidden animated:animated complete:block];
    
    return YES;
}


- (void)setStatusBarHidden:(BOOL)hidden{
    _statusBarHidden = hidden;
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        [self setNeedsStatusBarAppearanceUpdate];
    }
    else{
        [[UIApplication sharedApplication] setStatusBarHidden:hidden];
    }
}

- (BOOL)prefersStatusBarHidden{
    return _statusBarHidden;
}

- (void)cancel{
    if ([self.navigationController.viewControllers count] > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}

- (void)setTitle:(NSString *)title{
    [super setTitle:title];
    if (self.sgCustomNavigationBar) {
        [self.sgCustomNavigationBar setTitle:title];
    }
}

- (NSString *)sgNavBarTitle{
    if (self.sgCustomNavigationBar) {
        return self.sgCustomNavigationBar.title;
    }
    return nil;
}


- (UIStatusBarStyle)preferredStatusBarStyle{
    if (self.sgCustomNavigationBar.superview) {
        if (self.sgCustomNavigationBar.style == DefaultStyle) {
            return UIStatusBarStyleDefault;//黑字
        }
//        else if (self.sgCustomNavigationBar.style == LifeStyle){
//            return UIStatusBarStyleLightContent;//白字
//        }else if (self.sgCustomNavigationBar.style == LifeSubStyle){
//            return UIStatusBarStyleDefault;//黑字
//        }
//        else if (self.sgCustomNavigationBar.style == BookStyle){
//            return UIStatusBarStyleLightContent;//白字
//        }
        else{
            return UIStatusBarStyleLightContent;//白字
        }
    }
    return UIStatusBarStyleDefault;//黑字
}


@end
